package com.qingluan.server;

import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.server.BroadCastRecevier.ReceiveListener;

import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class BackgroundServer  extends Service{
	private  String TAG = BackgroundServer.class.getName();
	private WebSocketConnection connection;
	private String wsUri;
	private BroadCastRecevier broadCast ;
	private BroadCastRecevier broadCast_remove_one ;
	
	private IntentFilter filter ;
	private IntentFilter filter_remove_one;
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d(TAG, "onCreate() executed" );
		wsUri = ARGUMENTS.PUSH_INFO_IP;
		
		filter = new IntentFilter();
		filter.addAction(ARGUMENTS.SEND_INFO_TO_SERVICE);
		
		broadCast = new BroadCastRecevier();
		registerReceiver(broadCast, filter);
		
//		broadCast_remove_one = new BroadCastRecevier();
//		filter_remove_one = new IntentFilter();
//		filter_remove_one.addAction(ARGUMENTS.REMOVE_ONE_TO_SERVICE);
//		registerReceiver(broadCast_remove_one, filter_remove_one);
	}
	
	
	
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		if (broadCast == null){
			Log.d("Broad","BroadCast null...");
			broadCast = new BroadCastRecevier();
			registerReceiver(broadCast, filter);
		}
		
		if (connection != null){
			startConnection();
			Log.d("Sta", "START .....");
		}else{
			connection =  new WebSocketConnection();
			startConnection();
			
			Log.d("Sta", "START .....again..");
		}
		if (connection == null){
			Log.d("FAGE","No this ...");
		}else {
			
		}
//		broadCast_remove_one.setReceiveListenr(new ReceiveListener() {
//			
//			@Override
//			public void receive(String info) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
		
	}
	

	private void startConnection(){
		connection = new WebSocketConnection();
		
		try{
			connection.connect(wsUri, new WebSocketHandler(){
				@Override
				public void onOpen() {
					// TODO Auto-generated method stub
//					Intent intent  = new Intent();
//					intent.setAction(ARGUMENTS.PUSH_SERVICE_NAME);
//					intent.putExtra("MessagePush", "ON");
					Log.d("Sta","Web  socket connecting ... \n IP : "+wsUri);
//					sendBroadcast(intent);
					
					

				}
				
				@Override
				public void onTextMessage(String payload) {

					Log.d("Sta","Get message from  IP : "+wsUri +"\nMessage : "+payload);
					// TODO Auto-generated method stub
					Intent intent  = new Intent();
					intent.setAction(ARGUMENTS.PUSH_SERVICE_NAME);
					intent.putExtra("MessagePush", payload);
					sendBroadcast(intent);
					
					
					broadCast.setReceiveListenr(new ReceiveListener() {
						
						@Override
						public void receive(String info) {
							// TODO Auto-generated method stub
							Log.d("SHOU", info);
							if (connection.isConnected()){
								connection.sendTextMessage(info);
							}else {
								Log.d("Connect", "Connecting failed..");
								while (! connection.isConnected()){
									try {
										wait(1000);
										Log.d("re Connecting .., ","re connecting ");
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}
						}
					});
					
					
				}
				
				@Override
				public void onClose(int code, String reason) {
					// TODO Auto-generated method stub

					Log.d("Sta","Web  socket Stop \n IP : "+wsUri);
					Intent intent  = new Intent();
					intent.setAction(ARGUMENTS.PUSH_SERVICE_NAME);
					intent.putExtra("MessagePush", "OFF");
					sendBroadcast(intent);
					
				}
			});
		}catch( WebSocketException e ){
			Log.d("WebSocketError",e.toString());
		}
	}
}
